﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace LogAnalyzer
{
    public static class Util
    {
        public static List<LogMessage> LoadLog(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            var log = new List<LogMessage>();
            foreach (var fi in di.GetFiles())
            {
                if (fi.Extension == ".log")
                {
                    var stream = fi.OpenText();
                    while (!stream.EndOfStream)
                    {
                        var json = stream.ReadLine();
                        var logEntry = new JavaScriptSerializer().Deserialize<LogMessage>(json);
                        log.Add(logEntry);
                    }
                }
            }

            return log;
        }

        public static string AddZero(string s)
        {
            return "0" + s;
        }

        public static class MessageType
        {
            public static string Route = "Route";
            public static string Start = "Start";
            public static string Join = "Join";
            public static string RouteResponse = "RouteResponse";
            public static string JoinResponse = "JoinResponse";
            public static string StartSending = "StartSending";
        }

        public static string Proc(string node)
        {
            if(App.Nodes.Count > App.global)
                return node;

            return null;
        }
    }
}
